<template>
  <a-modal
    title="编辑数据库信息表"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="组织架构id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入组织架构id" v-decorator="['orgId', {rules: [{required: true, message: '请输入组织架构id！'}]}]" />
        </a-form-item>
        <a-form-item
          label="数据库权限分级"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入数据库权限分级" v-decorator="['dbPermissions', {rules: [{required: true, message: '请输入数据库权限分级！'}]}]" />
        </a-form-item>
        <a-form-item
          label="数据库名称（英文名称）"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入数据库名称（英文名称）" v-decorator="['dbName', {rules: [{required: true, message: '请输入数据库名称（英文名称）！'}]}]" />
        </a-form-item>
        <a-form-item
          label="jdbc的驱动类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入jdbc的驱动类型" v-decorator="['jdbcDriver', {rules: [{required: true, message: '请输入jdbc的驱动类型！'}]}]" />
        </a-form-item>
        <a-form-item
          label="jdbc的驱动版本号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入jdbc的驱动版本号" v-decorator="['jdbcDriverVersion', {rules: [{required: true, message: '请输入jdbc的驱动版本号！'}]}]" />
        </a-form-item>
        <a-form-item
          label="数据库连接的账号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入数据库连接的账号" v-decorator="['userName', {rules: [{required: true, message: '请输入数据库连接的账号！'}]}]" />
        </a-form-item>
        <a-form-item
          label="数据库连接密码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入数据库连接密码" v-decorator="['password', {rules: [{required: true, message: '请输入数据库连接密码！'}]}]" />
        </a-form-item>
        <a-form-item
          label="jdbc的url"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入jdbc的url" v-decorator="['jdbcUrl', {rules: [{required: true, message: '请输入jdbc的url！'}]}]" />
        </a-form-item>
        <a-form-item
          label="实时获取或者离线获取"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入实时获取或者离线获取" v-decorator="['online', {rules: [{required: true, message: '请输入实时获取或者离线获取！'}]}]" />
        </a-form-item>
        <a-form-item
          label="数据库结构变化数量"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入数据库结构变化数量" v-decorator="['dbChange', {rules: [{required: true, message: '请输入数据库结构变化数量！'}]}]" />
        </a-form-item>
        <a-form-item
          label="备注，摘要"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入备注，摘要" v-decorator="['remarks', {rules: [{required: true, message: '请输入备注，摘要！'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { databaseInfoEdit } from '@/api/modular/main/databaseinfo/databaseInfoManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      edit (record) {
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              orgId: record.orgId,
              dbPermissions: record.dbPermissions,
              dbName: record.dbName,
              jdbcDriver: record.jdbcDriver,
              jdbcDriverVersion: record.jdbcDriverVersion,
              userName: record.userName,
              password: record.password,
              jdbcUrl: record.jdbcUrl,
              online: record.online,
              dbChange: record.dbChange,
              remarks: record.remarks
            }
          )
        }, 100)
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            databaseInfoEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败')//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
